#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

size_t my_strlen(const char *str)
{
    if (str == nullptr)
        return 0;

    int count = 0;
    for (int i = 0; str[i] != '\0'; i++)
        ++count;

    return count;
}

char *my_strncpy(char *destination, const char *source, size_t num)
{
    if (destination == nullptr || source == nullptr)
        return nullptr;

    char *ptr_dest = destination;
    while (num > 0 && *source != '\0')
    {
        *(ptr_dest++) = *(source++);
        num--;
    }

    while (num > 0)
    {
        *(ptr_dest++) = '\0';
        num--;
    }

    return destination;
}

char *my_strcat(char *destination, const char *source)
{
    if (destination == nullptr || source == nullptr)
        return nullptr;

    char *ptr_dest = destination + strlen(destination);
    for (const char *ptr = source; (*ptr) != '\0'; ptr++)
        *(ptr_dest++) = *ptr;

    *ptr_dest = '\0';

    return destination;
}

char *my_strncat(char *destination, const char *source, size_t num)
{
    if (destination == nullptr || source == nullptr)
        return nullptr;

    char *ptr_dest = destination + strlen(destination);
    while (num > 0 && *source != '\0')
    {
        *(ptr_dest++) = *(source++);
        num--;
    }

    *ptr_dest = '\0';

    return destination;
}

int my_strcmp(const char *str1, const char *str2)
{
    if (str1 == nullptr && str2 == nullptr)
        return 0;
    if (str1 == nullptr || str2 == nullptr)
        return (str1 == nullptr ? -1 : 1);

    while (*str1 && *str2 && (*str1 == *str2))
    {
        ++str1;
        ++str2;
    }

    return *str1 - *str2;
}

int my_strncmp(const char *str1, const char *str2, size_t num)
{
    if (str1 == nullptr && str2 == nullptr)
        return 0;
    if (str1 == nullptr || str2 == nullptr)
        return (str1 == nullptr ? -1 : 1);

    while (num > 0 && *str1 && *str2 && (*str1 == *str2))
    {
        ++str1;
        ++str2;
        --num;
    }

    if (num == 0)
        return 0;
    return *str1 - *str2;
}

// char *my_strtok(char *str, const char *delimiters)
// {
//     static char *temp = nullptr;

//     if (str != nullptr)
//     {
//         //越过开头的分隔符
//         int i;
//         for (i = 0; str[i] != '\0'; ++i)
//             if (!strchr(delimiters, str[i]))
//                 break;

//         char *head = str + i;
//         str = str + i;
//         while (*str != '\0')
//         {
//             for (const char* ptr = delimiters; *ptr != '\0'; ++ptr)
//             {
//                 if (*str == *ptr)
//                 {
//                     *str = '\0';
//                     temp = str + 1;
//                     while (*temp != '\0' && strchr(delimiters, *temp))
//                         temp++;
//                     return head;
//                 }
//             }
//             ++str;
//         }
//         return head;
//     }
//     else
//     {
//         if (temp == nullptr || *temp == '\0')
//             return nullptr;

//         char *head = temp;
//         while (*temp != '\0')
//         {
//             for (const char* ptr = delimiters; *ptr != '\0'; ++ptr)
//             {
//                 if (*temp == *ptr)
//                 {
//                     *temp = '\0';
//                     temp = temp + 1;
//                     while (*temp != '\0' && strchr(delimiters, *temp))
//                         temp++;
//                     return head;
//                 }
//             }
//             ++temp;
//         }
//         temp = nullptr;
//         return head;
//     }
// }

char *my_strtok(char *str, const char *delimiters)
{
    static char *temp = nullptr;

    //统一用temp处理
    if (str != nullptr)
        temp = str;

    // 越过前面的分隔符
    while (*temp != '\0' && strchr(delimiters, *temp))
        ++temp;

    char *head = temp;

    //找到第一个分隔符
    while (*temp != '\0' && !strchr(delimiters, *temp))
        ++temp;

    if (*temp != '\0')
    {
        *temp = '\0';
        ++temp;
        return head;
    }

    //走到这里，说明已经走到了尾，直接返回nullptr
    return nullptr;
}

int main()
{
    // char str[] = {'a', '\0', 'a'};
    // cout << my_strlen(str) << endl;

    // char dest[20] = "aaaa";
    // char source[10] = "aaaab";
    // cout << my_strncmp(dest, source, 5) << endl;

    char str[] = "@@@@@@hh???h@@@@@@@@@@@@333?qq.com?124#@@#@@25#aaaaa@1234#########";
    char seq[] = "@.#$?";
    char _str[100] = {0};

    strcpy(_str, str);

    char *temp = my_strtok(_str, seq);

    for (; temp != NULL; temp = my_strtok(NULL, seq))
    {
        printf("%s\n", temp);
    }

    return 0;
}